//
//  XDWebPageMgrViewController.m
//  XDEducation
//
//  Created by apple on 2017/5/2.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDWebPageMgrViewController.h"

#import "XDWebPageMgrCell.h"
#import "XDWebPageMgr.h"
#import "XDSingleBtnCell.h"

#import "XDAddWebPageMgrViewController.h"

@interface XDWebPageMgrViewController ()

/** 列表数据模型 */
@property(nonatomic,strong) NSMutableArray *webPageMgrModelArr;
@end

static NSString *cellId = @"WebPageMgrCell";

@implementation XDWebPageMgrViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initNetworkingWithRefreshStatus:RefreshStatePullDown];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"XDWebPageMgrCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellId];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableView) name:@"dismissVC" object:nil];
}


-(NSMutableArray *)webPageMgrModelArr{

    if (!_webPageMgrModelArr) {
        _webPageMgrModelArr = [NSMutableArray array];
    }
    return _webPageMgrModelArr;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.webPageMgrModelArr.count + 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame = CGRectMake(15, 10, 60, 20);
    titleLabel.text = @"网页名称";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = UIColorFromRGB(0x4c4c4c);
    [view addSubview:titleLabel];
    
    UILabel *contentLabel = [[UILabel alloc]init];
    contentLabel.frame = CGRectMake(ScreenW/2 - 30, 10, 60, 20);
    contentLabel.text = @"网页地址";
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.textColor = UIColorFromRGB(0x4c4c4c);
    [view addSubview:contentLabel];
    
    UILabel *operationLabel = [[UILabel alloc]init];
    operationLabel.frame = CGRectMake(ScreenW - 15 - 30, 10, 30, 20);
    operationLabel.text = @"操作";
    operationLabel.font = [UIFont systemFontOfSize:14];
    operationLabel.textColor = UIColorFromRGB(0x4c4c4c);
    [view addSubview:operationLabel];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == self.webPageMgrModelArr.count )  return 70 * ScaleHeight;
    return 60 * ScaleHeight;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == self.webPageMgrModelArr.count) {
        
        XDSingleBtnCell *cell = [XDSingleBtnCell cellWithTableView:tableView];
        cell.title = @"添加网址";
        WeakSelf(weakself)
        cell.buttonClickedBlock = ^(){  //添加网址block
            
            XDAddWebPageMgrViewController * vc = [[XDAddWebPageMgrViewController alloc]init];
            weakself.definesPresentationContext = YES;
            vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            [weakself presentViewController:vc animated:YES completion:nil];
            
//            XDAddWebPageMgrViewController *vc = [[XDAddWebPageMgrViewController alloc]init];
//            [self presentViewController:vc animated:YES completion:nil];
        };
        cell.separatorInset = UIEdgeInsetsMake(0, ScreenW, 0, 0);
        return cell;
        
    }else{
    
        XDWebPageMgrCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        cell.webPageMgr = self.webPageMgrModelArr[indexPath.row];
        WeakSelf(weakself)
        //删除网址block
        cell.deleteBlock = ^(NSNumber *deleteId){
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除?" preferredStyle:UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [weakself deleteNetwork:deleteId];
            }]];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self presentViewController:alert animated:YES completion:nil];
            
        };
        return cell;
    }
}

-(void)initNetworkingWithRefreshStatus:(RefreshState)status{
    
    NSString *url = BaseURL(@"internet.do?action=getSetting");
    
    NSDictionary *dict = @{@"uid":[AccountTool shareAccountTool].currentAccount.uid};
    
    [MBProgressHUD showHUD];
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        [MBProgressHUD hideHUD];
        //NSLog(@"%@",responseObj);
        NSArray *dictArr = [responseObj objectForKey:@"t"];
        
        if (![dictArr isKindOfClass:[NSNull class]]) {
            
            NSMutableArray *tempArr = [XDWebPageMgr mj_objectArrayWithKeyValuesArray:dictArr];
            self.webPageMgrModelArr = tempArr;
            [self.tableView reloadData];
            
        }else{
            
            [MBProgressHUD showError:XDNoData];
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:XDNetWorkError];
        NSLog(@"%@",error);
    }];
}


-(void)deleteNetwork:(NSNumber *)deleteId{
    
    NSDictionary *dict = @{@"id":deleteId};
    
    NSString *url = BaseURL(@"internet.do?action=delSetting");
    
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
       
        NSLog(@"%@",responseObj);
        if ([[[responseObj objectForKey:@"code"] stringValue] isEqualToString:@"1000"]) {
            [self reloadTableView];
        }
        
    } failure:^(NSError *error) {
        
         NSLog(@"%@",error);
    }];
  
}

-(void)reloadTableView{
    
    [self initNetworkingWithRefreshStatus:RefreshStatePullDown];
}

@end
